package ch10_IO流与文件.缓冲流;

import java.io.*;

/**
 * @author 挚爱之夕
 * @date 2021/8/27 - 08 - 27 - 13:47
 * @Description: ch10.缓冲流
 * @Version: 1.0
 */
public class BufferedReaderWriteDemo {
    public static void main(String[] args) throws IOException {
        File dest = new File("E:/Test.txt");    //待写入和读入的文件

        //以下为写入文件
        int ch = '一';//要写入的第一个字符
        String line;//存放写入或读入的一行字符串
        try (FileWriter out = new FileWriter(dest);
             BufferedWriter bw = new BufferedWriter(out)) {
            for (int i = 0; i < 5; i++) {//5行
                line = "";
                for (int j = 0; j < 24; j++) {
                    line += ((char) (ch++) + " ");//拼接字符串并准备下一个字符
                }
                bw.write(line);//写出字符串
                bw.newLine();//写出换行字符
                bw.flush();//每写一行，刷新缓冲输出流
            }
        }

        //以下为读入文件
        try (FileReader in = new FileReader(dest);
             BufferedReader br = new BufferedReader(in)) {
            while((line = br.readLine())!=null){    //每次读入一行
                System.out.println(line);
            }
        }
    }

}
